//
//  HWRequestLogManage
//
//  Created by 李含文 on 2021/4/28.
//  Copyright © 2021 MINISO. All rights reserved.
//

#import "HWLogViewModel.h"
#import "HWRequestLogManage.h"

@implementation HWLogModel

- (CGFloat)getCellH {
    CGFloat cellH = 70;
    if (self.vcName.length) {
        cellH += 20;
        if (self.vctitle.length)
            cellH += 20;
    }
    return cellH;
}
@end

@implementation HWLogViewModel

+ (HWLogViewModel *)shared {
    static HWLogViewModel * viewModel = nil;
    static dispatch_once_t once;
    dispatch_once(&once,^{
        viewModel = [[self alloc] init];
    });
    return viewModel;
}

- (void)addModel:(HWLogModel *)model {
    [self.models insertObject:model atIndex:0];
    [[NSNotificationCenter defaultCenter] postNotificationName:k_HWLogViewModel_DataUpdate object:nil];
}

- (void)addstart_time:(NSString *)start_time
           end_time:(NSString *)end_time
              vc_nmae:(NSString *)vc_nmae
             vc_title:(NSString *)vc_title
          status_code:(NSString *)status_code
              request:(NSURLRequest *)request
       responseObject:(id)responseObject error:(NSError * __nullable)error {
    HWLogModel *model = [HWLogModel new];
    model.vcName = vc_nmae;
    model.vctitle = vc_title;
    model.HTTPMethod = request.HTTPMethod;
    model.responseObject = responseObject;
    model.error = error;
    model.request = request;
    if (start_time.length) {
        model.starttime = [start_time doubleValue];
        model.timeString = [self getTimeString:model.starttime];
    }
    if (end_time.length)
        model.endtime = [end_time doubleValue];
    
    model.url = request.URL.absoluteString;
    model.header = [NSString stringWithFormat:@"%@", request.allHTTPHeaderFields];
    model.body = [[NSString alloc] initWithData:request.HTTPBody encoding:(NSUTF8StringEncoding)];
    
    NSString *content = @"";
    if (error) {
        content = [NSString stringWithFormat:@"%@", error.description];
        model.code = -1;
    }
    
    if (responseObject) {
        if (status_code.length)
        model.code = [status_code integerValue];
        content = [NSString stringWithFormat:@"%@", responseObject];
        if (HWRequestLogManage.shared.successBlock) {
            if (HWRequestLogManage.shared.successBlock(responseObject))
                model.code = success_Code;
            else
                model.code = model.code;
        }
    }
    model.content = content;
    model.cellH = [model getCellH];
    [self addModel:model];
}

- (void)cleanData {
    [self.models removeAllObjects];
    [[NSNotificationCenter defaultCenter] postNotificationName:k_HWLogViewModel_DataUpdate object:nil];
}

- (NSString *)getTimeString:(NSTimeInterval)time {
    NSDate *detailDate=[NSDate dateWithTimeIntervalSince1970:time];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; //实例化一个NSDateFormatter对象
    [dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"]; //设定时间格式,这里可以设置成自己需要的格式
    return [dateFormatter stringFromDate: detailDate];
}

- (NSMutableArray<HWLogModel *> *)models {
    if (!_models)
        _models = [NSMutableArray array];
    return _models;
}

- (NSString *)hw_FilterNullString:(id)string {
    if ([string isEqual:@"NULL"] || [string isKindOfClass:[NSNull class]] || [string isEqual:[NSNull null]] || [string isEqual:NULL] || [[string class] isSubclassOfClass:[NSNull class]] || string == nil || string == NULL || [string isKindOfClass:[NSNull class]] || [[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0 || [string isEqualToString:@"<null>"] || [string isEqualToString:@"(null)"])
        return @"";
    else
        return (NSString *)string;
}

@end
